//index.js
//获取应用实例
const app = getApp()
const Api = require("../../utils/api.js").API
const Store = require("../../utils/store.js").store
const codeZ = require("../../utils/util.js")
Page({
  data: { 
    userInfo: {}, 
    isLogin: false,
    startBar:20,
    sum:0,
    custNum: 0,
    orderNum: 0,
    totalProfitAmt: 0,
    userStatus:'',
    serviceShow: false
  },
  onShow:function(){
      
    let baseinfo =  wx.getStorageSync('baseInfo');
       if(baseinfo.hasOwnProperty('ckType')){
           this.setData({
               userStatus:baseinfo.ckType
           })
       }else{
           this.setData({
               userStatus:''
           })
       }
        
      
      // wx.setTabBarItem({
      //    "index":4,
      // "pagePath": "pages/MyCustPage/index",
      // "iconPath": "images/MyNo.png",
      // "selectedIconPath": "images/MyNo.png",
      // "text": "我的"
      //  })
      
      
      
      wx.setStorageSync('shuaxin','');
      let that = this;
      let data = {
	"couponGrantChannel": "",
	"couponName": "",
	"couponNo": "",
	"couponType": "",
	"useState": "100001"
}
       Api.getCkCouponListApi(data).then(res=>{
           
           if(res.code == 0){
               var arr =  res.data; 
               
               that.setData({
                   sum:arr.length
               })
              }
                    
         
           });
      
      this.setData({
          startBar:app.globalData.statusBarHeight,
      })
      
  var token = Store.getStore('userInfo');
  
  
  
    if (token) { 
      this.setData({
      userInfo: Store.getStore('userInfo'),
      isLogin: true
    })
    this.getStatistics()
    }
  },
  goLogin:function(){
    wx.navigateTo({
         url: '../login/index',
    }   
  )    
  },
  myService: function() {
    this.setData({
        serviceShow: true
    })
  },
  onClose: function() {
    this.setData({
        serviceShow: false
    })
  },
  completemessage: function() {
      wx.showToast({
        title: '添加成功，客服会马上联系您',
        icon: 'none'
      })
  },
  getStatistics: function() {
    let _this = this
    Api.getStatisticsApi({}).then(res=>{
        console.log(res)
        if (res.code == 0) {
            _this.setData({
                custNum: res.data.custNum,
                orderNum: res.data.orderNum,
                totalProfitAmt: res.data.totalProfitAmt
            })
        }
        
    });
  },
  goLP:function(){
      if(!this.data.isLogin){
          return;
      }
      wx.navigateTo({
          url: '../myLpCard/index',
      })
  },
  goAddre:function(){
       
      if(!this.data.isLogin){
          return;
      }
      wx.navigateTo({
          url: '../adressList/index',
      })
  },
  goYQM:function(){
      if(!this.data.isLogin){
          return;
      }
      wx.navigateTo({
          url: '../yaoqingma/index',
      })
  },
  protocal:function(){
      if(!this.data.isLogin){
          return;
      }
      
       wx.navigateTo({
            url: '../webView/index',
            // success: function(res) {
            //   res.eventChannel.emit('acceptURLFromMyPage', { url: 'https://www.freshlogic.cn/agreement.htm'})
            // }
          })
  },
  goSC:function(){
      if(!this.data.isLogin){
          return;
      }
      wx.navigateTo({
           url: '../MyShouChange/index',
      })   
  },
  goSD:function(){
      if(!this.data.isLogin){
          return;
      }
      wx.navigateTo({
           url: '../shouDao/index',
      })   
  },
  goZF:function(){
      if(!this.data.isLogin){
          return;
      }
      wx.navigateTo({
           url: '../Detail/index?type='+'my',
      })   
  },
  goSLR:function(){
      if(!this.data.isLogin){
          return;
      }
      wx.navigateTo({
           url: '../MyShouLiRen/index',
      })   
  },

  toMyCust: function() {
    if(!this.data.isLogin){
        return;
    }
    wx.navigateTo({
        url: '../MyCust/index',
    })
  },
  
  goYH:function(){
      if(!this.data.isLogin){
          return;
      }
      wx.navigateTo({
          url: '../MyCoupons/index?zhi="1"',
      })
      // wx.navigateTo({
      //      url: '../login/index',
      // })   
     // wx.showToast({
     //   icon: 'none',
     //   title: '敬请期待'
     // })
  },
  goActive:function(){
      
  },
  clearLogin: function() {
    wx.clearStorage({
        // success(res) {
        //     app.globalData.sum = 0;
        //     codeZ.cart_badge(0);
        //     that.setData({ isLogin: false ,userInfo:{}})
        //     wx.navigateTo({
        //          url: '../login/index',
        //     })  
        //   }
    })
    this.setData({
        userInfo: {},
        isLogin: false
      })
  },

    logout: function(){
        if(!this.data.isLogin){
            return;
        }
        
         // Store.getStore('token');
       
          
        
       var that = this;
       wx.clearStorage({
         success(res) {
           app.globalData.sum = 0;
           codeZ.cart_badge(0);
           that.setData({ isLogin: false ,userInfo:{}})
           wx.navigateTo({
                url: '../login/index',
           })   
           
           
         }
       }) 
     },
     makePhoneCall:function(){
         // var phone = Store.getStore('dePoint')
         // if(!phone.hasOwnProperty('telNo')){
         //   return  wx.showToast({
         //         icon: 'none',
         //         title: '未获取到客服电话!'
                 
         //     })
             
         // }
        
         if(!this.data.isLogin){
             return;
         }
           wx.makePhoneCall({
              phoneNumber:"028-68091639",
              success:function(){
               
              },
              fail:function(){
                
                // console.log('拨打失败')
              }
           })
       
       },
       cheakAllOrder:function(e){
           if(!this.data.isLogin){
               return;
           }
           
           
           var a = e.currentTarget.dataset.id;
           if(a == '2'){
               wx.navigateTo({
                   url: '../MyCaiGou/index',
               })
           }else{
               wx.navigateTo({
                   url: '../MyOrderNew/index?type='+a,
               })
           }
        
          
           
       },
       
       jumpHuojia:function(){
           if(!this.data.isLogin){
               return;
           }
           wx.navigateTo({
             url: '../MyHuoJia/index',
           })
        //    wx.switchTab({
        //         url: '../MyHuoJia/index',
        //    })   
       },
       jumpMyShouChange:function(){
           if(!this.data.isLogin){
               return;
           }
           wx.navigateTo({
                url: '../MyShouChangeNew/index',
           })   
       },
       
     
})
